using Amazon.ECS.Model;
using System;
using RomanJendrusz.Amazon.ETL.Infrastructure.Loging.Core;

namespace RomanJendrusz.Amazon.ETL.Messages.MessageMappers
{
    public class GetBookReviewsCountMapper: MessageMapper<GetBookReviewsCountRequest, ItemLookupRequest, ItemLookupResponse>
    {
        public override AmazonResponse MapToAmazonResponse(ItemLookupResponse awsResponse)
        {
            var bookItem = GetRequestItem(awsResponse, 0);
            if(bookItem.IsSetCustomerReviews()== false)
            {
                LogGateway.LogFor(GetType()).Error(string.Format("The item with {0} doesn't have any customer reviews", bookItem.ASIN));
                return new GetItemsCountResponse(CorrelationId) {ItemsCount= 0 };
            }
            return new GetItemsCountResponse(CorrelationId){ItemsCount = Convert.ToInt32(bookItem.CustomerReviews.TotalReviews)};
        }

        public override ItemLookupRequest MapToAwsRequest(GetBookReviewsCountRequest request)
        {
            var awsRequest = new ItemLookupRequest { MerchantId = request.MerchantId };
            awsRequest.ItemId.Add(request.BookAsin);
            //awsRequest.IdType = "ISBN";
            //awsRequest.SearchIndex = "Books";
            awsRequest.ResponseGroup.Add(request.ResponseGroup);
            awsRequest.MerchantId = request.MerchantId;
            return awsRequest;
        }
    }
}